<template>
  <!-- 放置弹层组件 -->
  <el-dialog
    :title="form.id ? '编辑部门' : '新增部门'"
    :visible="showDialog"
    width="45%"
    @close="close"
  >
    <!-- 新增部门表单 -->
    <el-form ref="fm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <!--label选项显示的值 value:选中某个label的值 -->
          <el-option
            v-for="item in manager"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- 具名插槽 -->
    <span slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="addDept">新增</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees.js'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments.js'
export default {
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    // 是否显示
    showDialog: {
      type: Boolean,
      default: false
    },
    // 所有的信息
    allTreeDate: {
      type: Array,
      default: () => []
    },
    // 点击当前的数据信息
    currentNode: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    // 判断当前值是全局唯一
    const validateCode = (rule, value, cb) => {
      /**
       * 新增和编辑状态校验规则的处理
       * 1.新增=>不存在的一个
       */
      let flag
      if (this.form.id) {
        // 编辑
        // 用来修改看你修改的code是否与item.code一样
        flag = this.allTreeDate.some(item => value === item.code && value !== this.currentNode.code)
      } else {
        // 新增
        flag = this.allTreeDate.some(item => value === item.code)
      }
      if (flag) {
        cb(new Error('当前code码重复'))
      } else {
        cb()
      }
    }
    return {
      manager: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求2-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      allTreeData: {
        type: Array,
        default: () => []
      }
    }
  },
  created () {
    this.getAllMessage()
  },
  methods: {
    // 关闭弹层
    close () {
      console.log(1)
      this.$emit('close-dialog')
      // 手动清空=>编辑部门功能需要=>清除多余数据
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      // 清空校验和数据
      /**
       * 注意:
       * 1.没有校验规则的数据不能清除
       * 2.多出来的数据不能清除
       */
      this.$refs.fm.resetFields()
    },
    // 新增
    addDept () {
      this.$refs.fm.validate(async (isOk) => {
        if (isOk) {
          if (this.form.id) {
            await updateDepartments(this.form)
            this.$message.success('更新成功')
          } else {
            await addDepartments({ ...this.form, pid: this.currentNode.id || '' })
            this.$message.success('添加成功')
          }

          this.$emit('update-depart')
          this.close()
        }
      })
    },
    async getAllMessage () {
      const res = await getEmployeeSimple()
      this.manager = res
      console.log(res)
    },
    // 编辑
    async getDept (id) {
      const res = await getDepartDetail(id)
      console.log('编辑的:', res)
      this.form = res
    }
  }
}
</script>
<style lang="sass" scoped>
</style>
